package com.gaotu.demo.jvm;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * java -Xmx20m  -Xmn4m
 */
@RequestMapping("heap")
@RestController
public class HeapController {
    private static final int LENGTH_M = 1024 * 1024;
    private final List<Object> data = new ArrayList<>();

    @RequestMapping("normal/{loop}")
    public String normal(@PathVariable("loop") int loop){
        List<Object> data = new ArrayList<>();
        for (int i=0; i< loop; i++){
            data.add(createData());
        }
        return "SUCCESS";
    }

    @RequestMapping("oom/{loop}")
    public String oom(@PathVariable("loop") int loop){
        for (int i=0; i<loop; i++){
            this.data.add(createData());
        }

        return "SUCCESS";
    }

    private Object createData(){
        return new byte[LENGTH_M];
    }
}
